<?php

namespace App\Http\Model;

use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

class ColorGiftRateConfig extends Base{

    protected $colorGiftRateConfigTable = 'color_gift_rate_config';//彩蛋礼物奖池
    protected $giftTable = 'gift_library';//礼物表
    protected $gift_stream = 'gift_stream';//礼物流水表
    protected $colorGiftRandStreamTable = 'color_gift_rate_award_stream';//彩蛋礼物中奖记录
    protected $colorGiftMaxConfigTable = 'color_gift_rate_max_config';//彩蛋最大流出流入配置

    protected $colorGiftRateLog = 'error_colorGiftRate';
    protected $colorGiftRateRedis = 'COLOR:GIFT:RATE:LIST:'; //string 彩蛋奖池列表 COLOR:GIFT:RATE:LIST:{giftID}=>{json}
    protected $colorGiftMaxConfig = 'COLOR:GIFT:MAX:CONFIG';//string 彩蛋配置最大奖池限制 COLOR:GIFT:MAX:CONFIG 

    public function __construct(array $attributes = [])
    {
        parent::__construct($this->table, $attributes);

    }

    //配置彩蛋礼物奖池
    public function setColorGiftPool($giftId, $childGiftId, $rate, $poolId){
        try{
            DB::beginTransaction();
            $giftInfo = DB::table($this->giftTable)->where('gift_id', $childGiftId)->first();
            $giftInfo = objToArray($giftInfo);
            $param = [
                'parent_gift_id' => $giftId,
                'gift_id' => $childGiftId,
                'rate' => $rate,
                'gift_price' => $giftInfo['price'],
                'gift_name' => $giftInfo['name'],
                'created_at' => Carbon::now()->toDateTimeString(),
            ];
            if(!empty($poolId)){
                DB::table($this->colorGiftRateConfigTable)->where('id',$poolId)->update($param);
                Redis::DEL($this->colorGiftRateRedis.$giftId);
            }else{
                DB::table($this->colorGiftRateConfigTable)->insert($param);
            }
            
            DB::commit();
            return true;
        }catch(\Exception $e){
            DB::rollBack();
            write_log($this->colorGiftRateLog,'ERROR 彩蛋礼物配置奖池',['param'=>$param,'msg'=>$e->getMessage(),'line'=>$e->getLine(),'file'=>$e->getFile()]);
            return false;
        }
    }

    //获取彩蛋礼物奖池信息
    public function getColorGiftPoolByGiftId($giftId){
        //获取礼物信息
        $giftPool = Redis::EXISTS($this->colorGiftRateRedis.$giftId);
        if(empty($giftPool)){
            //生成概率数据
            $poolList = DB::table($this->colorGiftRateConfigTable)->where('parent_gift_id',$giftId)->get();
            $poolList = objToArray($poolList);
            if(!empty($poolList)){
                Redis::SET($this->colorGiftRateRedis.$giftId,json_encode($poolList));
            }else{
                return false;
            }
        }
        return true;
    }

    //彩蛋礼物抽奖记录
    public function colorRandLog($param){
        $id = DB::table($this->colorGiftRandStreamTable)->insert($param);
        return $id;
    }

    //获取配置奖项中最大流出金额和流入金额
    public function getTodayMax(){
        $config = Redis::get($this->colorGiftMaxConfig);
        if(!empty($config)){
            return json_decode($config,true);
        }
        $data = DB::table($this->colorGiftMaxConfigTable)->where('status',1)->first();
        $data = objToArray($data);
        if(empty($data)){
            return [];
        }
        Redis::SET($this->colorGiftMaxConfig,json_encode($data));
        return $data;
    }

    //获取今日奖品的流入金额和流出金额
    public function getTodayInOut(){
        $start = Carbon::today()->startOfDay()->toDateTimeString();
        $end = Carbon::today()->endOfDay()->toDateTimeString();
        //流入金额
        $inDaimond = DB::table($this->gift_stream)->select(DB::raw("SUM(gift_price*gift_num) as total")) ->where('gift_type',5)->where('change_timestamp','>=',strtotime($start))->where('change_timestamp','<=',strtotime($end))->get();
        $inDaimond = objToArray($inDaimond);
        //流出金额
        $outDaimond = DB::table($this->colorGiftRandStreamTable)->select(DB::raw("SUM(gift_num*award_gift_price) as total"))->where('created_at','>=',$start)->where('created_at','<=',$end)->get();
        $outDaimond = objToArray($outDaimond);
        $data = [
            'inDiamond' => empty($inDaimond[0]['total']) ? 0 :$inDaimond[0]['total'] ,
            'outDiamond' => empty($outDaimond[0]['total']) ? 0 : $outDaimond[0]['total'],
        ];
        return $data;
    }

    //彩蛋礼物设置上线值
    public function setColorGiftMax($maxOut,$maxIn){
        $check = DB::table($this->colorGiftMaxConfigTable)->where('id',1)->exists();
        $data = [
            'max_out' => $maxOut,
            'max_in' => $maxIn,
            'status' =>1,
        ];
        if($check){
            $data['updated_at'] = date("Y-m-d H:i:s",time());
            DB::table($this->colorGiftMaxConfigTable)->where('id',1)->update($data);
        }else{
            $data['created_at'] = date("Y-m-d H:i:s",time());
            DB::table($this->colorGiftMaxConfigTable)->insert($data);
        }
        Redis::DEL($this->colorGiftMaxConfig);
        return true;
    }
}